package com.njupt.HashTable;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

//leetcode  242.有效的字母异位词
public class IsAnagram {

    public boolean isAnagram(String s, String t) {

        if(s.length()!=t.length()){
            return false;
        }
        Map<Character, Integer> ss = new HashMap<>();
        Map<Character, Integer> tt = new HashMap<>();

        for (int i = 0; i <s.length() ; i++) {
            char c = s.charAt(i);
            char c1 = t.charAt(i);
            if(ss.containsKey(c)){
                int num = ss.get(c) + 1;
                ss.put(c,num);
            }else {
                ss.put(c,1);
            }

            if (tt.containsKey(c1)) {
                int i1 = tt.get(c1) +1;
                tt.put(c1,i1);
            }else {
                tt.put(c1,1);
            }
        }

        for (Map.Entry<Character,Integer> entry: ss.entrySet()){
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        System.out.println();
        System.out.println();

        for (Map.Entry<Character,Integer> entry: tt.entrySet()){
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }


        Iterator<Character> iterator = ss.keySet().iterator();
        while (iterator.hasNext()){
            Character next = iterator.next();
            if(ss.get(next)!=tt.get(next)){
                return false;
            }
        }


        return true;
    }

    public boolean isAnagram1(String s, String t){

        if(s.length()!=t.length()){
            return false;
        }
        int[] record = new int[26];

        for (int i = 0; i < s.length(); i++) {
           record[s.charAt(i)-'a']++;
           record[t.charAt(i)-'a']--;
        }

        for (int num:record){
            if(num != 0){
                return false;
            }
        }
        return true;
    }




    public static void main(String[] args) {

        String s = "hhbywxfzydbppjxnbhezsxepfexkzofxyqdvcgdvgnjbvihgufvgtuxtpioxgjuwawkbaerbsirrktcjcesepcocaglbassivfbjhikynfsvlbtk" +
                "awstrclbkpuldshfgfvwjawownulsggsxhhqglbhjlgltfrqyjntgldlgorgatnjrsywlyngxrcwfojkmydmjbzuquqnzbpwlcbdzgdicctpkgtbdj" +
                "gywkyrkityvohjbuvmzdfyicyplmrpygdhaeqbuxnehfybsysnnmzbhprsyjmtpcrzeqqbirofzjtlsyofdyeffkvpuhzaflwjfhnsuyryetjuajjnj" +
                "wvlvrhvpenaarnzoafztixjrisfzdlepcwhxudjpsiirtofymnovacjmpdjtethjqfwduekczlqhsfjgqesyoxcfooagrdhyvsmssbhsclnlblhobvhwtpyftolne" +
                "ozlhbtjagpgqnnapktyevdvjfwdnbwsbelweoflhyhifprieuvfcdkavqxkygjlaegqfmzndgxbsccjgpclxmlpstrqjtqyvlqcopnahqvnpvkjimfdlosvletmamqj" +
                "votqwhadutmfvlgldniixvdkmymfadckuaglgbuttymoqmzkaeqxugsrnfyxzcamwxujgzupefretsvbdweuwwcizjvhcowtmwgkdafcpzctpsjwdocgofivyshwdinbd" +
                "hbxdfhjsrrsfchxkeqndgzauyprwfnrbmunanqnhmjhrufoinakwaciaoerioqffmipfqujfxwofqdyjbhagkyvmnxcwomgnmwlaodxgkgthnuctoozxrebjiynjwohtguk" +
                "yneyofabpfdrxklopmxxiwjuxqpaazkns" +
                "cagfiaetmmwnwpzceglupqvlctywtpluoqbzdultcsudubqclbwlxyfboimfwriugfulbntvdwnxgiycxvennakpodorvpiknkridcumsovvfziki...";

        String t = "rmhdxtymfgxjluxmeeerhxrrtjgecmyfdhevakyosvvuwgbwmxiakbmtqjjpgctjojdqatuelqmfeldgoprxbzcylajhhjngq" +
                "lmporofjpoyhrkptrbpzvvnkrqjovfzmmqybqxzjxarhguqcyvlugwmnzwtffanzuajraqbvjflgalgcwuhmrkblobybwouffzptbvkm" +
                "woklczfhvkqsirkqvhkryyxpkyfnwnjudvllmznupldkpoplwdlfzncwxdlgvgkehpcudqybnyhofpjnjmahnsrntxjtgocwsrnukejehwz" +
                "xdetglgahdvzyypdztqwbnfrnytxprhtqjgobzjyewuasgnryjoyvwhdmemppixqcowgmicbmsukgodwgnsdieuqyzuysedmsphmritlqtnvnv" +
                "fnmgnghxdsmmnzdhdalhfsfsppupteyrlajxrlbyqteaytgpsckottqnibbxhmkwuqfftwvibzsnaumsbohxeoaulsmwuvjcimscyqhtioekembd" +
                "wfozglussscbzejqnvmcpfwdmxshhrkquacplctruyklkxcfovcbqllmyghdbkhdmawpmalrsgymgmbleftwtvldxkqprcbcbneyyluoyydtcvkim" +
                "qjoinwntfxfbwqjbufqansdlgrgauzfwxpzrjxpesmozcsczbfzfeziedqzwywtxhyfnpbxfgbqcsfwdiplegcsrseqsagvkpjrupkaxffrddebpnt" +
                "ocmkbarvdrhvujedwnktsdimzcbqlqyizozxcexqpoefjpznegsrtbfanqdyqaaudcklplvxojfuzccmtauaxurbwbexulqbxooadcrlxbtggxcycadrat" +
                "suqavwwkdqtutptdpbnxeicmackhyvj" +
                "hkfbayjmymjwejybhoryyqjjmwqzhpgulovxaccychffcplvkqfqghmgmdsigkugsevwkvzgpxiolhtzocdiphokfitgg...";

//        int count = 0;
//        for (int i=0;i<t.length();i++){
//            if(s.charAt(i)=='a'){
//                count++;
//            }
//        }
//        System.out.println(count);

        IsAnagram test = new IsAnagram();

        System.out.println(test.isAnagram1(s, t));
    }
}
